package cn.thoughtworks.school.controllers;

import cn.thoughtworks.school.annotations.Auth;
import cn.thoughtworks.school.entities.User;
import cn.thoughtworks.school.entities.commands.CreateSurveyQuizCommand;
import cn.thoughtworks.school.entities.commands.UpdateSurveyQuizCommand;
import cn.thoughtworks.school.handlers.BusinessException;
import cn.thoughtworks.school.services.QuizService;
import cn.thoughtworks.school.services.SurveyQuizService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/api/surveyQuizzes")
public class SurveyQuizController {
    @Autowired
    protected SurveyQuizService surveyQuizService;
    @Autowired
    private QuizService quizService;

    @GetMapping(value = "pageable")
    public ResponseEntity getSurveyQuizzesPageable(@PageableDefault(sort = {"id"}, direction = Sort.Direction.DESC) Pageable pageable,
                                                   @RequestParam(required = false) Long quizId,
                                                   @RequestParam(required = false) Long userId,
                                                   @RequestParam(required = false) String description,
                                                   @RequestParam(required = false) List<String> tags,
                                                   @Auth User user) {
        Page surveyCodingQuizzesPageable = surveyQuizService.getSurveyCodingQuizzesPageable(pageable, quizId, userId, description, tags, user);
        return ResponseEntity.ok(surveyCodingQuizzesPageable);
    }

    @PostMapping()
    public ResponseEntity createSurveyQuiz(@RequestBody CreateSurveyQuizCommand createSurveyQuizCommand,
                                           @Auth User user) {
        surveyQuizService.createSurveyQuiz(createSurveyQuizCommand, user);
        return new ResponseEntity(HttpStatus.CREATED);
    }

    @DeleteMapping("/{quizId}")
    public ResponseEntity delSurveyQuiz(@PathVariable Long quizId) throws BusinessException {
        surveyQuizService.delete(quizId);
        return new ResponseEntity(HttpStatus.NO_CONTENT);
    }

    @GetMapping("/{quizId}")
    public ResponseEntity getById(@PathVariable Long quizId) throws BusinessException {
        return ResponseEntity.ok(surveyQuizService.findByIdOrThrowException(quizId));
    }

    @PutMapping("/{quizId}")
    public ResponseEntity updateSurveyQuiz(@RequestBody UpdateSurveyQuizCommand updateSurveyQuizCommand,
                                           @PathVariable Long quizId,
                                           @Auth User user) throws BusinessException {
        surveyQuizService.updateSurveyQuiz(quizId, updateSurveyQuizCommand, user);
        return new ResponseEntity(HttpStatus.NO_CONTENT);
    }

    @GetMapping(value = "/selecting/{ids}")
    public ResponseEntity getQuizzes(@PathVariable String ids) {
        return ResponseEntity.ok(surveyQuizService.getQuizzesByIds(ids));
    }

    @GetMapping("/students/{studentId}/assignments/{assignmentId}/quizzes/{ids}")
    public ResponseEntity getSurveyQuizzesSubmission(@PathVariable Long studentId, @PathVariable Long assignmentId, @PathVariable String ids) {
        return ResponseEntity.ok(surveyQuizService.getSurveyQuizzesSubmission(studentId, assignmentId, ids));
    }


    @PostMapping("/students/{studentId}/assignments/{assignmentId}/quizzes")
    public ResponseEntity submitBasicQuiz(@PathVariable Long studentId,
                                          @PathVariable Long assignmentId,
                                          @RequestBody List<Map> data) {
        surveyQuizService.submitAnswer(studentId, assignmentId, data);

        return new ResponseEntity(HttpStatus.CREATED);
    }

    @PostMapping("yml")
    public ResponseEntity ymlImport(@RequestParam("file") MultipartFile file,
                                    @Auth User current) throws BusinessException, IOException {
        if (validateYmlZipFormat(file)) {
            quizService.ymlImport(file, current);
            return new ResponseEntity(HttpStatus.CREATED);
        }
        throw new BusinessException("Wrong format");
    }

    private boolean validateYmlZipFormat(MultipartFile file) {
        String fileName = file.getOriginalFilename();
        return fileName.endsWith(".zip");
    }
}


